package com.hoongs.v1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoongs.v1.dto.SettingsPageDto;
import com.hoongs.v1.entity.Settings;
import com.hoongs.v1.mapper.SettingsMapper;
import com.hoongs.v1.service.SettingsService;
import com.hoongs.v1.utils.PResult;
import com.hoongs.v1.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;

@Service
public class SettingsServiceImpl implements SettingsService {

    public static final String NOT_FOUND_ARTICLE = "没有找到该项设置！";

    @Resource
    private SettingsMapper settingsMapper;

    @Override
    public Result add(Settings settings) {
        if (settingsMapper.insert(settings) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result remove(String id) {
        if (!findArticleById(id)) return Result.opError(NOT_FOUND_ARTICLE);
        if (settingsMapper.deleteById(id) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result update(Settings settings) {
        if (!findArticleById(settings.getId())) return Result.opError(NOT_FOUND_ARTICLE);
        if (settingsMapper.updateById(settings) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result selectById(String id) {
        Settings settings = settingsMapper.selectById(id);
        if (settings == null) return Result.error(NOT_FOUND_ARTICLE);
        return Result.success(settings);
    }

    @Override
    public Result selectByProp(String prop) {
        Settings settings = settingsMapper.selectOne(new QueryWrapper<Settings>().eq("prop", prop));
        if (settings == null) return Result.error(NOT_FOUND_ARTICLE);
        return Result.success(settings);
    }

    @Override
    public Result selectList(String name,String prop) {
        QueryWrapper<Settings> wrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(name)) {
            wrapper.like("name", name);
        }
        if (StringUtils.isNotBlank(prop)) {
            wrapper.like("prop", prop);
        }
        Collection<Settings> list = settingsMapper.selectList(wrapper);
        return Result.success(list);
    }

    @Override
    public Result selectPageList(SettingsPageDto settingsDto) {
        if (!PResult.checkPageQuery(settingsDto.getPage(), settingsDto.getSize())) return Result.error("分页参数错误");

        Page<Settings> daoPage = new Page<>(settingsDto.getPage(), settingsDto.getSize());
        QueryWrapper<Settings> wrapper = new QueryWrapper();
        IPage<Settings> settingsDaoIPage = settingsMapper.selectPage(daoPage, wrapper);
        PResult rs = new PResult(settingsDaoIPage);
        return Result.success(rs);
    }

    /**
     * 根据Id查询配置是否存在
     */
    public Boolean findArticleById(String id) {
        Settings settings = settingsMapper.selectById(id);
        return settings != null;
    }
}
